Package servlets

Source Code of servlets.Admin

package servlets;

import classes.User;
import classes.DAO;

import java.io.IOException;
import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* When the green button Admin is clicked, the user is redirected to the Admin
* login page
* @author Sandu Andreea & Morozan Ion
*/
public class Admin extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param req Servlet request
     * @param res Servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        getServletContext().getRequestDispatcher("/WEB-INF/admin.jsp").forward(req, resp);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request Servlet request
     * @param response Servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String userType = "Admin";

        /* create new user */
        User user = new User(username, password, userType);

        /* instance of database */
        DAO dao = DAO.getInstance();

        /* all fields must be filled && pass must match */
        if (username.isEmpty() || password.isEmpty() || userType == null
                || !dao.exists(user.id, username)
                /*extract the pass from the DB and compare with the one that
                 the user enters now*/
                || !password.equals(((User) dao.get(user.id, username)).password)) {
           
            HttpSession session = request.getSession(false);
            session.removeAttribute("username");
            session.removeAttribute("userType");
           
            request.setAttribute("errorMessage", "<font size=\"3\" "
                    + "face=\"arial\" color=\"red\">"
                    + "Invalid username or password. Please try again! </font>");
            RequestDispatcher rd = request.getRequestDispatcher("admin.jsp");
            rd.forward(request, response);
        } else {
                HttpSession session = request.getSession(true);
                session.setAttribute("username", username);
                session.setAttribute("userType", userType);

                RequestDispatcher rd = request.getRequestDispatcher("adminPanel.jsp");
                rd.forward(request, response);
                response.sendRedirect("adminPanel.jsp");
            }
        }
}
TOP

Related Classes of servlets.Admin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.